# Choose the data we wanna know
# ave HR/power/cadence ; normalized power ; max avg power 20 min max power for performance evaluation
# ascent and distance use to see the effort
"  Variables:
activity$Total.Ascent
activity$Distance
"
## [1] "  Variables:\nactivity$Total.Ascent\nactivity$Distance\n"
" bar for distance"
## [1] " bar for distance"
# for label
totals_d <- activity %>%
    group_by(Month) %>%
    summarize(total = sum(Distance))

#pop up
HL_lot_num <- c("2020-12","2021-02","2021-04")

Highlight <- activity %>% 
        filter(Month %in% HL_lot_num)

# plot
p1_dis <- ggplot(data = activity) + 
  geom_col(aes(Month,Distance),fill = "#7bccc4",size = 1.5)+
  geom_col(aes(Month,Distance),data = Highlight,fill = "#e34a33",size = 1.5)+
  ggtitle("Bar plot - Monthly distance")+
  labs(x="Month",y="Distance (KM)")+
  geom_hline(aes(yintercept = sum(Distance)/nlevels(Month)), linetype = "longdash", color = "red",size = 1)+
  geom_text(aes(Month,total,label = total),data = totals_d,vjust=-0.5) +
  scale_y_continuous(breaks = c(0,100,200,236.77,300,400,500))+
  theme_minimal()+
  annotate(geom = "text", x = 2, y = 255, label = "Ave.Distance",color = "red") ;p1_dis

"bar for ascent"
## [1] "bar for ascent"
# for label
totals_asc <- activity %>%
    group_by(Month) %>%
    summarize(total = sum(Total.Ascent))

p1_asc <- ggplot(data = activity) + 
  geom_col(aes(Month,Total.Ascent),fill = "#addd8e",size = 1.5)+
  geom_col(aes(Month,Total.Ascent),data = Highlight,fill = "#e34a33",size = 1.5)+
  ggtitle("Bar plot - Monthly Ascendt")+
  labs(x="Month",y="Ascent (Meter)")+
  geom_hline(aes(yintercept = sum(Total.Ascent)/nlevels(Month)), linetype = "longdash", color = "red",size = 1)+
  geom_text(aes(Month,total,label = total),data = totals_asc,vjust=-0.5)+
  scale_y_continuous(breaks = c(0,1000,2000,2635.5,3000,4000,5000))+
  theme_minimal()+
  annotate(geom = "text", x = 2, y = 2800, label = "Ave.Ascent",color = "red") ;p1_asc

p1_dis + p1_asc                                                                                                                    

"  Variables:                                                                                                           
Analyze performance
activity$Avg.HR
activity$Avg.Bike.Cadence
activity$Avg.Power
activity$Max.Avg.Power..20.min.
activity$Normalized.Power...NP..
activity$Max.Power
"
## [1] "  Variables:                                                                                                           \nAnalyze performance\nactivity$Avg.HR\nactivity$Avg.Bike.Cadence\nactivity$Avg.Power\nactivity$Max.Avg.Power..20.min.\nactivity$Normalized.Power...NP..\nactivity$Max.Power\n"
# Calculate difference
diff_power <- activity %>%
    group_by(Month) %>%
    summarize(Avg_HR = mean(Avg.HR),Avg_NP = mean(Normalized.Power...NP..))

diff_power$diff <- -diff_power$Avg_HR + diff_power$Avg_NP

p2_HR <- ggplot(activity) + 
  geom_boxplot(aes(Month,Avg.HR),fill = "#fcbba1",outlier.shape = 4)+
  geom_jitter(aes(Month,Avg.HR),width = 0.1 )+
  geom_line(data = diff_power,aes(Month,Avg_HR,group = 1),linetype ="twodash",color ="red",size = 2)+
  coord_cartesian(ylim=c(120,160)) ;p2_HR

p2_power <- ggplot(activity)+
  geom_boxplot(aes(Month,Normalized.Power...NP..),fill = "#99d8c9")+
  geom_jitter(aes(Month,Normalized.Power...NP..),width = 0.1)+
   geom_line(data = diff_power,aes(Month,Avg_NP,group = 1),linetype ="twodash",color ="red",size = 2);p2_power

p2_diff <- ggplot(diff_power)+
  geom_area(aes(Month,diff,group =1),fill = "#ccece6", color = "#006d2c")+
  geom_point(aes(Month,diff),color = "#006d2c",size =2)+
  geom_text(aes(Month,diff,label = ro(diff,2)),vjust=0,hjust=1.5,size = 4)+
  theme_classic()+
  geom_hline(aes(yintercept = mean(diff)), linetype = "longdash", color = "red",size = 1)+
  annotate(geom = "text", x = 2, y = 25, label = "Ave.diff = 20.98",color = "red") ;p2_diff

(p2_HR|p2_power)/p2_diff

# Layout
layout <- 
'
AAABBB
AAABBB
AAABBB
CCCCCC
'
p2_HR + p2_power + p2_diff + plot_layout(design = layout)

# gganimate max_20min power curve
ggplot(activity)+
  geom_point(aes(Date,Max.Avg.Power..20.min.))+
  geom_line(aes(Date,Max.Avg.Power..20.min.,group = 1),size = 0.7) +
  coord_cartesian(ylim=c(100,200))  +
  transition_reveal(along = Date)

"  Variables:
segment$rider
segment$SunshineHillclimbChallengeCourse_sec
segment$FourmiletoGoldHill_sec
segment$SuperFlag_sec
segment$w_kg_ratio
segment$SunshineHillclimbChallengeCourse_min
segment$FourmiletoGoldHill_min
segment$SuperFlag_min
"
## [1] "  Variables:\nsegment$rider\nsegment$SunshineHillclimbChallengeCourse_sec\nsegment$FourmiletoGoldHill_sec\nsegment$SuperFlag_sec\nsegment$w_kg_ratio\nsegment$SunshineHillclimbChallengeCourse_min\nsegment$FourmiletoGoldHill_min\nsegment$SuperFlag_min\n"
# point
ggplot(segment)+
  geom_point(aes(SuperFlag_min,FourmiletoGoldHill_min,color = w_kg_ratio_int))+
    theme_minimal()

# boxplot 
ggplot(segment)+
  geom_point(aes(SuperFlag_min,FourmiletoGoldHill_min,color = w_kg_ratio_int))+
  geom_boxplot(aes(SuperFlag_min,FourmiletoGoldHill_min,group = SuperFlag_min),fill = "#74c476")+
  theme_minimal()

# interaction
interaction_plot <- ggplot(segment)+ geom_point_interactive(aes(SuperFlag_min,FourmiletoGoldHill_min,color=w_kg_ratio_int,tooltip =rider ,data_id= w_kg_ratio_int))+scale_color_brewer(type = "qual",palette = "Accent") 

# Highlight by w/kg group
test_p <- ggplot(segment)+
  geom_point(aes(SuperFlag_min,FourmiletoGoldHill_min,color=w_kg_ratio_int),size = 1.5)

# filter 
ggplotly(test_p)
# interaction, hovering
girafe(ggobj=interaction_plot)